package
{
	import flash.net.SharedObject;
	
	import org.flixel.*;

	public class Music extends FlxSprite
	{
		private var localInformation:SharedObject;
		
		//Whether or not we're muted
		public static var MUTE:Boolean;
		
		private var playedOnce:Boolean;

		public function Music(x:int, y:int)
		{
			super(x,y,null);
			//676,0
			localInformation = SharedObject.getLocal("elementalGameInfo");
			var status:String = localInformation.data["music"];
			if (status != "mute") {
				localInformation.data["music"] = "play";
				localInformation.flush();
				this.loadGraphic(Res.notMuteImg);
				Music.MUTE = false;
			} else {
				this.loadGraphic(Res.muteImg);
				Music.MUTE = true;
			}
			playedOnce = false;
			
		}

		public function updateMusic(music:String = null):void{
			if (music == null)
				music = Elemental.CurrentPlay;
			
			if (FlxG.mouse.justPressed() && this.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				if (Music.MUTE) {
					playStreammed(music);
					localInformation.data["music"] = "play";
					localInformation.flush();
					this.loadGraphic(Res.notMuteImg);
					Music.MUTE = false;
				} else {
					if (FlxG.music != null && FlxG.music.active) {
						FlxG.music.stop();
					}
					localInformation.data["music"] = "mute";
					localInformation.flush();
					this.loadGraphic(Res.muteImg);
					Music.MUTE = true;
				}
			} else if (!playedOnce && !MUTE && Elemental.CurrentPlay != music){
				playStreammed(music);
				playedOnce = true;
			}
		}
		
		public function playStreammed(file:String):void{
			Elemental.CurrentPlay = file;
			FlxG.streamMusic(Res.musicPath+file);
		}
			
	}
}